package com.bvgol.examples.springboot.mybatismultiple.service.impl;

/**
 * @Classname BookServiceImpl
 * @Description TODO
 * @Date 2021/4/7 14:24
 * @Created by GUOCHEN
 */

import com.bvgol.examples.springboot.mybatismultiple.entity.Book;
import com.bvgol.examples.springboot.mybatismultiple.mapper.dataSource1.BookDao;
import com.bvgol.examples.springboot.mybatismultiple.service.BookService;
import lombok.AllArgsConstructor;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: my-spring-completely
 * @description:
 * @author: GUOCHEN
 * @create: 2021/04/07 14:24
 */
@Service("bookService")
@AllArgsConstructor
public class BookServiceImpl implements BookService {

    private BookDao bookDao;

    @Override
    public List<Book> selectAll() {

        return bookDao.selectAll();
    }

    public void batchInsert(){
        //这种批量添加快点
        //foreach 一次性插20~50行数量是比较合适的，时间消耗也能接受。
//        https://mybatis.org/mybatis-dynamic-sql/docs/insert.html#batch-insert-support
        //#
//        SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH);
//        try {
//            BookDao mapper = session.getMapper(BookDao.class);
//            List<Book> records = getRecordsToInsert(); // not shown
//
//            BatchInsert<Book> batchInsert = insert(records)
//                    .into(simpleTable)
//                    .map(id).toProperty("id")
//                    .map(firstName).toProperty("firstName")
//                    .map(lastName).toProperty("lastName")
//                    .map(birthDate).toProperty("birthDate")
//                    .map(employed).toProperty("employed")
//                    .map(occupation).toProperty("occupation")
//                    .build()
//                    .render(RenderingStrategy.MYBATIS3);
//
//            batchInsert.insertStatements().stream().forEach(mapper::insert);
//
//            session.commit();
//        } finally {
//            session.close();
//        }
    }

}
